/**
 * Class EntityTypeModule.
 * 
 * @author Maxime Ollagnier
 */
EntityTypeModule.inherits(Module);
function EntityTypeModule() {
	var that = this;
	
	/** Entity types table module */
	this.entityTypeTable = new TableModule();
	this.entityTypeTable.id = 'entityTypeTable';
	this.entityTypeTable.setSelectable(true);
	this.entityTypeTable.setOnSelect(function() {
		var entityType = Util.getObject(that.entityTypeTable.selectedObject);
		that.inputName.setValue(entityType.name).buildJQ();
		that.inputName.validate();
	});
	// Name column
	this.entityTypeTable.columnList.push(new Column('name', I18n.get('name'), '90%', true));
	// Remove column
	var removeCol = new Column('remove', '', '10%');
	this.entityTypeTable.columnList.push(removeCol);
	removeCol.clickable = false;
	removeCol.getText = function(entityType) {
		if (Util.checkBoolTrue(entityType.deletable)) {
			return $('<i class="icon-remove icon"/>').click(function() { that.deleteEntityType(entityType); });
		}
	};
	
	/** Entity type input */
	this.inputName = new InputTextModule({
		'id' : 'inputName',
		'label' : I18n.get('entity.type.name'),
		'placeholder' : I18n.get('name'),
		'mandatory' : true,
		'onEnter' : function() {
			that.saveEntityType();
		},
		'validationPattern' : ValidationPattern.VALID_DEFAULT,
		'validationMethod' : new ValidationMethod({
			'errorMessage' : I18n.get('entity.type.name.taken'),
			'method' : function(value) {
				if (Util.checkObject(that.entityTypeTable.selectedObject)) {
					return true;
				}
				var exists = false;
				that.entityTypeTable.objectMap.foreach(function(id, entityType) {
					exists = exists || (entityType.name == value);
				});
				return !exists;
			}
		})
	});
}

EntityTypeModule.prototype.fillJQ = function(jQ) {
	var that = this;
	this.inputName.setValue();
	
	jQ.append($('<h4>' + I18n.get('entity.types') + '</h4>'));
	
	var jQDisplay = $('<div class="form well cropbottom row-fluid"></div>');
	jQ.append(jQDisplay);
	var columnLeft = $('<div class="span6"></div>');
	columnLeft.append(this.entityTypeTable.buildJQ());
	var columnRight = $('<div class="span6"></div>');
	columnRight.append(this.inputName.buildJQ());
	columnRight.append($('<span class="btn">' + I18n.get('save') + '</span>').click(function() { that.saveEntityType(); }));
	jQDisplay.append(columnLeft).append(columnRight);
	
	this.inputName.getJQ().find('input').focus();
};

/**
 * Loads the entity types
 */
EntityTypeModule.prototype.loadData = function(callback) {
	var that = this;
	AjaxManager.getJSON('getEntityTypes', {}, function(result) {
		if (result.success) {
			that.entityTypeTable.selectedObject = undefined;
			that.entityTypeTable.objectMap = new Map();
			// Refreshes as well the enum of Entity Types
			EnumManager.EntityType = {};
			$.each(result.entityTypes, function(index, entityType) {
				that.entityTypeTable.objectMap.put(index, entityType);
				EnumManager.EntityType[entityType.name] = entityType.name;
			});
		}
		Util.getFunction(callback)();
	});
};

EntityTypeModule.prototype.saveEntityType = function() {
	if (!this.validate()) {
		return;
	}
	var that = this;
	AjaxManager.getJSON('saveEntityType', {
		'id' : Util.getObject(this.entityTypeTable.selectedObject).id,
		'name' : this.inputName.value
	}, function(result) {
		if (result.success) {
			that.reload();
		}
	});
};

EntityTypeModule.prototype.deleteEntityType = function(entityType) {
	if (!Util.checkObject(entityType)) {
		return;
	}
	var that = this;
	AjaxManager.getJSON('deleteEntityType', {
		'id' : entityType.id
	}, function(result) {
		if (result.success) {
			that.reload();
		}
	});
};
